﻿@using Fluxor.Blazor.Web.Components;
@using SimpleIdServer.IdServer.UI;
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.ClientStore;
@using SimpleIdServer.IdServer.Website.Stores.UserStore;
@inherits FluxorComponent
@inject Radzen.DialogService dialogService
@inject NotificationService notificationService
@inject IOTPQRCodeGenerator otpQRCodeGenerator
@inject IState<AddClientState> addClientState
@inject IDispatcher dispatcher

@if (Credential.OTPAlg == OTPAlgs.HOTP)
{
    <p class="text-muted">@Global.HotpDescription</p>
}
else
{
    <p class="text-muted">@Global.TotpDescription</p>   
}

<!-- Counter -->
@if(Credential.OTPAlg == OTPAlgs.HOTP)
{
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Counter</RadzenText>
        <RadzenNumeric Name="Password" @bind-Value="@Credential.OTPCounter" Class="w-100" Disabled=true></RadzenNumeric>
    </div>
}
<!-- QR Code -->
<div>
    <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.QrCode</RadzenText>
    <RadzenImage Path="@image" Style="width: 200px" />
</div>

@code {
    [Parameter]
    public UserCredential Credential { get; set; }
    [Parameter]
    public User User { get; set; }
    string image;

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if (User == null) return;
        var payload = otpQRCodeGenerator.GenerateQRCode(User, Credential);
        image = $"data:image/jpeg;base64,{Convert.ToBase64String(payload)}";
    }
}